﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Sitecore.Data.Items;
using System.Data;
using Workflow.Alerts.DAL;
using Workflow.Alerts.Interfaces.ProfileSettings;

namespace Workflow.Alerts
{
    public class NetworkOperatorDtlsPresenter : Presenter
    {
        #region members
        private readonly NetworkOperatorDAL model;
        private readonly INetworkOperatorDtlsView view;
        #endregion

        #region constructor
        public NetworkOperatorDtlsPresenter(INetworkOperatorDtlsView _view)
            : this(_view, null)
        { }

        public NetworkOperatorDtlsPresenter(INetworkOperatorDtlsView _view, ISessionProvider session)
            : base(_view, session)
        {
            view = base.GetView<INetworkOperatorDtlsView>();
            view.OnPageLoad += view_OnLoad;
            view.OnUpdate += view_OnUpdate;
            model = new NetworkOperatorDAL();
        }

        #endregion

        #region private methods

        private void view_OnLoad()
        {
            var filter = new WorkflowFilter
            {
                NetworkOperatorId = view.Id,
                MunicipalityId = CurrentUser.MunicipalityId
            };

            var filterSql = new WorkflowFilterSql(filter);

            EstateDataSet.NetworkOperatorDataTable operators = model.GetNetworkOperatorDetails(filterSql);

            view.ContractorsChoiseList = WorkflowEnums.ConstuctorChoise;

            if (operators.Rows.Count == 1)
            {
                EstateDataSet.NetworkOperatorRow networkOperator = operators[0];
                view.Name = networkOperator.name;
                view.Code = networkOperator.code;
                view.PostAddress = networkOperator.postaddress;
                view.PostCode = networkOperator.postcode;
                view.City = networkOperator.postcity;
                view.IsDisabled = networkOperator.disabled;
                view.Login = networkOperator.login;
                view.Password = networkOperator.password;
                view.Email = networkOperator.email;
                view.FirstName = networkOperator.firstname;
                view.LastName = networkOperator.lastname;
                view.Phone1 = networkOperator.phone1;
                view.Phone2 = networkOperator.phone2;
                view.Notifications = networkOperator.notification;
                view.Confirmations = networkOperator.confirmation;
                view.ContractorsChoise = networkOperator.usemunicipconstractor ? 1 : 0;
            }
        }

        private bool view_OnUpdate()
        {
            var filter = new WorkflowFilter
            {
                Code = view.Code,
                Name = view.Name,
                PostAddress = view.PostAddress,
                PostCode = view.PostCode,
                PostCity = view.City,
                Login = view.Login,
                Password = view.Password,
                NetworkOperatorId = view.Id,
                Email = view.Email,
                Disabled = view.IsDisabled,
                MunicipalityId = CurrentUser.MunicipalityId,
                LastName = view.LastName,
                FirstName = view.FirstName,
                Phone1 = view.Phone1,
                Phone2 = view.Phone2,
                Notification = view.Notifications,
                Confirmation = view.Confirmations,
                UseMunicipConstractor = view.ContractorsChoise==1

            };

            WorkflowFilterSql filterSql = new WorkflowFilterSql(filter);

            PersonDAL dal = new PersonDAL();
            string exceptionField = string.Empty;
            if (!dal.ValidateUser(filterSql, ref exceptionField))
            {
                view.ExceptionField = exceptionField;
                return false;
            }

            if (view.Id.HasValue)
            {
                //update existing operator
                model.UpdateNetworkOperator(filterSql);
            }
            else
            {
                //create new operator                
                view.Id = model.CreateNetworkOperator(filterSql);
            }

            return true;
        }
        #endregion
    }
}
